package com.hqyj.springBoot.modules.test.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.hqyj.springBoot.modules.common.vo.Result;
import com.hqyj.springBoot.modules.common.vo.Search;
import com.hqyj.springBoot.modules.test.entity.City;
import com.hqyj.springBoot.modules.test.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CityController {

    @Autowired
    private CityService cityService;

    /**
     * 127.0.0.1/api/city -------post
     * {"cityName":"zjz","localCityName":"张济舟","countryId":522}
     */
    @PostMapping(value = "/city",consumes = MediaType.APPLICATION_JSON_VALUE)
    Result<City> insertCity(@RequestBody City city){
        return cityService.insertCity(city);
    }


    /**
     * 127.0.0.1/api/city -------put
     * cityId=2261,cityName=zhang,localCityName=张大帅哥,dateModified=2020-09-10 20:00:33
     */
    @PutMapping(value = "/city",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    Result<City> updateCity(@ModelAttribute City city){
        return cityService.updateCity(city);
    }


    /**
     * 127.0.0.1/api/city/cityId -----delete
     */
    @DeleteMapping(value = "/city/{cityId}")
    public Result<Object> deleteCity(@PathVariable int cityId) {
        return cityService.deleteCity(cityId);
    }


    /**
     * 127.0.0.1/api/city/2234  -----get
     */
    @GetMapping("/city/{cityId}")
    public City getCityByCityId(@PathVariable int cityId) {
        return cityService.getCityByCityId(cityId);
    }

    /**
     * 127.0.0.1/api/cities/522  -----get
     */
    @GetMapping(value = "/cities/{countryId}")
    public List<City> getCitiesByCountryId(@PathVariable int countryId) {
        return cityService.getCitiesByCountryId(countryId);
    }

    /**
     * 127.0.0.1/api/cities  -----post
     *{"currentPage":1,"pageSize":5,"sort":"city_id","direction":"desc","keyword":""}
     * 涉及到敏感数据,就要用bean包装到请求体里面封装起来
     *
     * 请求类型一致,请求路径一致,用v2,比如
     * 127.0.0.1/api/cities/v2
     */
    @PostMapping(value = "/cities",consumes = MediaType.APPLICATION_JSON_VALUE)
    public PageInfo<City> getCitiesBySearch(@RequestBody Search search){
        return cityService.getCitiesBySearch(search);
    }

    /**
     * 127.0.0.1/api/cities/v2
     */
    @PostMapping(value = "/cities/v2",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Page<City> getCitiesBySearch2(@RequestBody Search search){
        return cityService.getCitiesBySearch2(search);
    }

}
